/**
 * Created by wenchuan on 2018/5/5.
 */
define(function () {

    var self,
        app = win.app,
        userInfo,
        customersArg,
        cdata = {
            GWAK018: '',//线人电话
            GWAK017: '',//线人姓名
            GWAK022: '',
            GWAK023: '',
            GWAK024: '',
            GWAK025: '',
            GWAK026: '',
            GWAK028: '',
            GWAK030: '',
            GWAK031: '',
            GWAK055: '',
            GWAKL003: ''
        },
        provinces = [],
        citys = [],
        districts = []
    ;

    var CustomersAdd = app.Module.extend({

        init: function () {
            self = this;
            self._super({}, win);
            userInfo = app.mainPage.userInfo;
            cdata.GWAK017 = userInfo.ID;
            cdata.GWAK018 = userInfo.MOBILE;
            customersArg = app.pageParameters['customersadd_page'];
        },
        checkPhone: function () {

            app.ajax({
                xurl: "api/wx/gwat100/checkCustomerPhone",
                loading: true,
                xdata: {
                    GWAK030: cdata.GWAK030
                },
                cb: function (result) {
                    if (result.ok) {
                        var count = result.data.count;
                        if (count > 0) {
                            app.alert({
                                msg: '客户手机号已经存在'
                            });
                        } else {
                            self.subData();
                        }
                    }
                }
            });
        },
        subData: function () {

            app.ajax({
                xurl: "api/wx/gwat100/saveCustomer",
                loading: true,
                xdata: cdata,
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '添加成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                        customersArg.cb();
                    }
                }
            });
        },
        getProvince: function () {
            app.ajax({
                xurl: "api/wx/user/getProvinces",
                xdata: {},
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        cdata.GWAK023 = datas[0].GWAD002;
                        cdata.GWAK022 = datas[0].GWAD003;
                        provinces.pushArray(datas);
                        self.getCity();
                    }
                }
            });
        },
        getCity: function () {
            app.ajax({
                xurl: "api/wx/user/getCitys",
                xdata: {
                    GWAD003: cdata.GWAK023
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            cdata.GWAK024 = datas[0].GWAD002;
                            citys.pushArray(datas,true);
                            self.getDistricts();
                        } else {
                            citys.removeAll();
                            districts.removeAll();
                        }
                    }
                }
            });
        },
        getDistricts: function () {
            app.ajax({
                xurl: "api/wx/user/getDistricts",
                xdata: {
                    GWAD003: cdata.GWAK024
                },
                cb: function (result) {
                    if (result.ok) {
                        var datas = result.data.data;
                        if (datas.length > 0) {
                            cdata.GWAK025 = datas[0].GWAD002;
                            districts.pushArray(datas, true);
                        } else {
                            districts.removeAll();
                        }
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            self.getProvince();

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () {
            new Vue({
                el: doc.querySelector('.mui-content'),
                data: {
                    sub: cdata,
                    provinces: provinces,
                    citys: citys,
                    districts: districts
                },
                methods: {
                    provinceChange: function () {
                        self.getCity();
                        provinces.iFilter({
                            o:{GWAD002: cdata.GWAK023},
                            cb:function (arr,len) {
                                if(len>0){
                                    cdata.GWAK022 = arr[0].GWAD003;
                                    app.log(cdata.GWAK022);
                                }
                            }
                        });
                    },
                    cityChange: function () {
                        self.getDistricts();
                    },
                    addData: function () {
                        if (!cdata.GWAKL003 || cdata.GWAKL003 == '') {
                            app.toast('客户名称不能为空');
                            return;
                        }
                        if (!cdata.GWAK031 || cdata.GWAK031 == '') {
                            app.toast('联系人不能为空');
                            return;
                        }
                        if (!cdata.GWAK030 || cdata.GWAK030 == '') {
                            app.toast('手机号不能为空');
                            return;
                        }
                        if (!cdata.GWAK025 || cdata.GWAK025 == '') {
                            app.toast('区县不能为空');
                            return;
                        }
                        app.confirm({
                            msg: '是否保存客户信息？',
                            ok: function () {
                                self.checkPhone();
                            }
                        });
                    }
                }
            });
        }
    });

    return new CustomersAdd();

});